
package dialogs;

import core.COINCOMOComponent;
import core.COINCOMOSubSystem;
import core.COINCOMOSystem;
import core.COINCOMOUnit;
import database.COINCOMOComponentManager;
import database.COINCOMOSubSystemManager;
import database.COINCOMOSystemManager;
import extensions.COINCOMOTreeNode;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import main.GlobalMethods;
import main.Icons;
import panels.COCOTSoverviewPanelSS;
import panels.HierarchyPanel;
import panels.OverviewsAndGraphsPanel;

/**
 *
 * @author Raed Shomali
 */
public class ViewCotsProjectsDialog extends JDialog implements ActionListener
{
    Container c = getContentPane();

    JButton loadButton = new JButton( "Load" );

    //JButton deleteButton = new JButton( "Delete" );

    JButton closeButton = new JButton( "Close" );

    DefaultListModel listModel = new DefaultListModel();

    JList availableProjectList = new JList( listModel );

    JScrollPane listScroller = new JScrollPane( availableProjectList );

    public static Vector<String> allProjects = null;
    
    COINCOMOComponent component = null;
    /**
     * 
     * @param parentFrame is the original frame that generated this Dialog
     */
    public ViewCotsProjectsDialog( JFrame parentFrame )
    {
        super( parentFrame );
        
        this.setTitle( "View Projects..." );
        this.setModal( true );

        // Buttons
        loadButton.addActionListener( this );
        closeButton.addActionListener( this );

        loadButton.setFocusable( false );
        closeButton.setFocusable( false );

        loadButton.setIcon( Icons.LOAD_PROJECT_ICON );
        closeButton.setIcon( Icons.CLOSE_ICON );

        // List
        TitledBorder availableSystemsTitleBorder = BorderFactory.createTitledBorder( "Available Systems" );
        availableSystemsTitleBorder.setTitleColor( Color.DARK_GRAY );
        availableSystemsTitleBorder.setTitlePosition( TitledBorder.BELOW_TOP );
        availableSystemsTitleBorder.setTitleJustification( TitledBorder.CENTER );
        listScroller.setBorder( availableSystemsTitleBorder );

        // GUI
        c.setLayout( null );

        c.add( listScroller );

        c.add( loadButton );
        c.add( closeButton );

        listScroller.setBounds( 20 , 20 , 290 , 220 );

        loadButton.setBounds( 20 , 260 , 90 , 25 );
        closeButton.setBounds( 220 , 260 , 90 , 25 );

        GlobalMethods.updateStatusBar( "Loading Available Systems ..." ) ;
        
        loadButton.setEnabled( false ) ;
        closeButton.setEnabled( false ) ;
        
        // Update in a GUI Thread
        SwingUtilities.invokeLater( new Runnable()
        {
            public void run()
            {
                try
                {
                    // Rest a Bit ... So the GUI would Render
                    Thread.sleep( 100 );

                    allProjects = COINCOMOComponentManager.loadCocotsProjects();
                    //COINCOMOSubSystemManager.updateProjectResult

                    for ( int i = 0 ; i < allProjects.size() ; i++ )
                    {
                        String tempProject = allProjects.get( i );

                        // Add to the list
                        listModel.addElement( tempProject );
                    }

                    loadButton.setEnabled( true ) ;
                    closeButton.setEnabled( true ) ;
        
                    GlobalMethods.updateStatusBar( "Available Projects have been loaded." );
                }
                catch ( InterruptedException ex )
                {
                }
            }

        } );

        this.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
        this.setLocation( this.getOwner().getX() + 100 , this.getOwner().getY() + 100 );
        this.setResizable( false );
        this.setSize( 335 , 330 );
        this.setVisible( true );
    }
    
    public ViewCotsProjectsDialog( JFrame parentFrame, COINCOMOComponent component )
    {
        super( parentFrame );
        
        this.component = component;
        
        this.setTitle( "View Projects..." );
        this.setModal( true );

        // Buttons
        loadButton.addActionListener( this );
        closeButton.addActionListener( this );

        loadButton.setFocusable( false );
        closeButton.setFocusable( false );

        loadButton.setIcon( Icons.LOAD_PROJECT_ICON );
        closeButton.setIcon( Icons.CLOSE_ICON );

        // List
        TitledBorder availableSystemsTitleBorder = BorderFactory.createTitledBorder( "Available Systems" );
        availableSystemsTitleBorder.setTitleColor( Color.DARK_GRAY );
        availableSystemsTitleBorder.setTitlePosition( TitledBorder.BELOW_TOP );
        availableSystemsTitleBorder.setTitleJustification( TitledBorder.CENTER );
        listScroller.setBorder( availableSystemsTitleBorder );

        // GUI
        c.setLayout( null );

        c.add( listScroller );

        c.add( loadButton );
        c.add( closeButton );

        listScroller.setBounds( 20 , 20 , 290 , 220 );

        loadButton.setBounds( 20 , 260 , 90 , 25 );
        closeButton.setBounds( 220 , 260 , 90 , 25 );

        GlobalMethods.updateStatusBar( "Loading Available Systems ..." ) ;
        
        loadButton.setEnabled( false ) ;
        closeButton.setEnabled( false ) ;
        
        // Update in a GUI Thread
        SwingUtilities.invokeLater( new Runnable()
        {
            public void run()
            {
                try
                {
                    // Rest a Bit ... So the GUI would Render
                    Thread.sleep( 100 );

                    allProjects = COINCOMOComponentManager.loadCocotsProjects();
                    //COINCOMOSubSystemManager.updateProjectResult

                    for ( int i = 0 ; i < allProjects.size() ; i++ )
                    {
                        String tempProject = allProjects.get( i );

                        // Add to the list
                        listModel.addElement( tempProject );
                    }

                    loadButton.setEnabled( true ) ;
                    closeButton.setEnabled( true ) ;
        
                    GlobalMethods.updateStatusBar( "Available Projects have been loaded." );
                }
                catch ( InterruptedException ex )
                {
                }
            }

        } );

        this.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
        this.setLocation( this.getOwner().getX() + 100 , this.getOwner().getY() + 100 );
        this.setResizable( false );
        this.setSize( 335 , 330 );
        this.setVisible( true );
    }

    public void actionPerformed( ActionEvent e )
    {
        if ( e.getSource() == loadButton )
        {
        	component.setProjectName( allProjects.get(availableProjectList.getSelectedIndex()) );
        	COINCOMOComponentManager.loadCocotProject(component.getProjectName(), component);
        	this.dispose();
        }
       
        else
        {
            // Exit ...
            this.dispose();
        }
    }

   
}
